package com.qianfeng.dbsm.impl;

import com.qianfeng.dbsm.dao.TbProductCosmeticsDao;
import com.qianfeng.dbsm.dao.TbProductInventoryDao;
import com.qianfeng.dbsm.entity.TbProductCosmetics;
import com.qianfeng.dbsm.entity.TbProductInventory;
import com.qianfeng.dbsm.service.TbProductInventoryService;
import com.qianfeng.dbsm.vo.UpdateProductNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 库存量(TbProductInventory)表服务实现类
 *
 * @author makejava
 * @since 2022-11-18 19:53:08
 */
@Service("tbProductInventoryService")
public class TbProductInventoryServiceImpl implements TbProductInventoryService {
    @Resource
    private TbProductInventoryDao tbProductInventoryDao;

    @Autowired
    TbProductCosmeticsDao tbProductCosmeticsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param tbProductInventoryId 主键
     * @return 实例对象
     */
    @Override
    public TbProductInventory queryById(Integer tbProductInventoryId) {
        return this.tbProductInventoryDao.queryById(tbProductInventoryId);
    }

    /**
     * 分页查询
     *
     * @param tbProductInventory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TbProductInventory> queryByPage(TbProductInventory tbProductInventory, PageRequest pageRequest) {
        long total = this.tbProductInventoryDao.count(tbProductInventory);
        //return new PageImpl<>(this.tbProductInventoryDao.queryAllByLimit(tbProductInventory, pageRequest), pageRequest, total);
        return null;
    }

    /**
     * 新增数据
     *
     * @param tbProductInventory 实例对象
     * @return 实例对象
     */
    @Override
    public TbProductInventory insert(TbProductInventory tbProductInventory) {
        this.tbProductInventoryDao.insert(tbProductInventory);
        return tbProductInventory;
    }

    /**
     * 修改数据
     *
     * @param tbProductInventory 实例对象
     * @return 实例对象
     */
    @Override
    public TbProductInventory update(TbProductInventory tbProductInventory) {
        this.tbProductInventoryDao.update(tbProductInventory);
        return this.queryById(tbProductInventory.getTbProductInventoryId());
    }

    /**
     * 通过主键删除数据
     *
     * @param tbProductInventoryId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer tbProductInventoryId) {
        return this.tbProductInventoryDao.deleteById(tbProductInventoryId) > 0;
    }


    /**
     * 修改数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public UpdateProductNum updateProductNum(int productId, int inventoryId, int num) {
        //this.tbProductInventoryDao.update(tbProductInventory);
        UpdateProductNum upNum;
        TbProductCosmetics tbProductCosmetics = tbProductCosmeticsDao.queryById(productId);
        if (tbProductCosmetics != null) {
            if (tbProductInventoryDao.queryById(productId).getCapacity() > 0) {
                System.out.println("还剩" + tbProductInventoryDao.queryById(productId).getCapacity() + "可以购买!!");
                upNum = new UpdateProductNum();
                upNum.setSaleNum(tbProductCosmetics.getSaleNum() + 1);
                return upNum;
            }
        }
        return null;
    }
}
